package com.leyou.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.shop.entity.UpdateLog;
import com.leyou.shop.service.IUpdateLogService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.leyou.mall.data.entity.Result;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyb
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/update-log")
public class UpdateLogController {

    @Resource
    private IUpdateLogService updateLogService;

    /**
     * @description: 新增和修改
     * @author 车矣捕
     */
    @PostMapping
    @Transactional
    public Result save(@RequestBody UpdateLog updateLog) {
        updateLogService.saveOrUpdate(updateLog);
        return Result.success();
        }

    /**
     * @description: 删除
     * @author 车矣捕
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        updateLogService.removeById(id);
        return Result.success();
        }

    /**
     * @description: 批量删除
     * @author 车矣捕
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        updateLogService.removeByIds(ids);
        return Result.success();
        }

    /**
     * @description: 查询所有
     * @author 车矣捕
     */
    @GetMapping
    public Result findAll() {
        return Result.success(updateLogService.list());
    }

    /**
     * @description: 根据id查询
     * @author 车矣捕
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(updateLogService.getById(id));
    }

    /**
     * @description:  分页查询
     * @author 车矣捕
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize) {
        QueryWrapper<UpdateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(updateLogService.page(new Page<>(pageNum, pageSize), queryWrapper));
        }

}

